package com.source.multithread.download;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Enumeration;



/*import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;*/

public class IPDemo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
      System.out.println(getLocalIp());
      
      
      System.out.println(new Timestamp(System.currentTimeMillis()));  // 2020-01-21 10:16:33.681
      System.out.println(new Date(System.currentTimeMillis()));  // Tue Jan 21 10:16:33 CST 2020
      
      System.out.println(isBlank("  2"));
	}
	
	/**
	 * 获取当前服务器的IP地址
	 */
	
	
	public static boolean isBlank(String str){
	    int strLen;
	    if ((str == null) || ((strLen = str.length()) == 0)){
	      return true;
	    }
	    for (int i = 0; i < strLen; i++) {
	      if (!Character.isWhitespace(str.charAt(i))) {
	        return false;
	      }
	    }
	    return true;
	}
	
    public   static String getLocalIp(){
    	String  localIp="127.0.0.1";
    	InetAddress ip = null;
    	try {
    		Enumeration<NetworkInterface>  interfaces=	NetworkInterface.getNetworkInterfaces();
    		while(interfaces.hasMoreElements()){
    			NetworkInterface   ni = interfaces.nextElement();
    			Enumeration<InetAddress> inetAddresses =	ni.getInetAddresses();
    			while(inetAddresses.hasMoreElements()){
    			InetAddress inetAddress=	inetAddresses.nextElement();
    			if(!inetAddress.isLoopbackAddress()&& (inetAddress instanceof  Inet4Address)){
    				ip=inetAddress;
    			}
    			}
    		}
    		if(null!=ip){
    			localIp=ip.getHostAddress();
    		}
    		
		} catch (SocketException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
    	return localIp;
    	
    }
	
	
   /* public static String getLoaclIp(){
        String localIp = "127.0.0.1";
        InetAddress ip = null;
        // 根据网卡获取IP
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) interfaces
                        .nextElement();
                Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
                        ip = inetAddress;
                    }
                }
            }
            if(null!=ip){
                localIp = ip.getHostAddress();
            }
        } catch (SocketException e) {
        ///	logger.info("000012",e);
        }
        return localIp;
    }*/

}
